
package com.fram.service.admin.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fram.bean.admin.user.request.UserAdminSearchRequest;
import com.fram.bean.admin.user.response.UserRoleResponse;
import com.fram.bean.sys.PageBean;
import com.fram.common.canstant.ErrCode;
import com.fram.common.exception.ProException;
import com.fram.common.exception.ProRunTimeException;
import com.fram.entity.AdminUserRole;
import com.fram.entity.User;
import com.fram.mapper.AdminUserRoleMapper;
import com.fram.mapper.UserMapper;
import com.fram.service.admin.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private UserMapper userMapper;
	@Autowired
	private AdminUserRoleMapper adminUserRoleMapper;

	// 查询所有用户
	@Override
	public PageInfo<User> selectUser(PageBean<User> request) {
		PageHelper.startPage(request.getPageNum(), request.getPageSize());
		List<User> list = userMapper.selectUser(request.getParam());
		if (list == null || list.size() == 0) {
			throw new ProRunTimeException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return new PageInfo<>(list);
	}

	//
	@Override
	public User selectById(Long id) throws ProException {
		User user = userMapper.selectByPrimaryKey(id);
		if (user == null) {
			throw new ProRunTimeException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return user;
	}

	@Override
	public void updateUser(User record) throws ProException {
		if (userMapper.updateByPrimaryKeySelective(record) == 0) {
			throw new ProRunTimeException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
	}

	/****/
	@Override
	public void updateRole(AdminUserRole request) throws ProException {
		if (request.getId() != null) {
			if (adminUserRoleMapper.updateByPrimaryKeySelective(request) == 0) {
				throw new ProRunTimeException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
			}
		} else {
			if (adminUserRoleMapper.insertSelective(request) == 0) {
				throw new ProRunTimeException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
			}
		}
	}

	/****/
	@Override
	public PageInfo<UserRoleResponse> listUserRole(PageBean<UserAdminSearchRequest> request) throws ProException {
		PageHelper.startPage(request.getPageNum(), request.getPageSize());
		List<UserRoleResponse> list = adminUserRoleMapper.selectUserRole(request.getParam());
		if (list == null || list.size() == 0) {
			throw new ProRunTimeException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return new PageInfo<>(list);
	}

	@Override
	public AdminUserRole details(Long id) throws ProException {
		AdminUserRole adminUserRole = adminUserRoleMapper.selectByPrimaryKey(id);
		if (adminUserRole == null) {
			throw new ProRunTimeException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return adminUserRole;
	}
}
